package DAO.imp;

import DAO.BaseAdminDao;
import com.Express.util.DruidUtil;

import java.sql.*;
import java.util.Date;

public class AdminDaoMysql implements BaseAdminDao {
 private static  final  String SQL_UPDATE_LOGIN_TIME="UPDATE eadmin SET LOGINTIME=? WHERE USERNAME=?";
    private static final String SQL_LOGIN = "SELECT  id FROM eadmin WHERE username=? AND password=? ";

    @Override
    public void updateLoginTime(String username, Date date, String ip) {
        //1,获取连接
        Connection connection=null;
        connection = DruidUtil.getConnection();
        PreparedStatement state=null;
       //2  预编译SQL语句
        try {
            //3 填充参数
            state = connection.prepareStatement(SQL_UPDATE_LOGIN_TIME);
            state.setDate(1,new java.sql.Date(date.getTime()));
            //state.setString(2,ip);
            state.setString(2,username);
            state.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //释放资源
           DruidUtil.close(connection,state,null);
        }
    }

    @Override
    public boolean login(String username, String password) {
        //1,获取连接
        Connection connection=null;
        connection = DruidUtil.getConnection();
        PreparedStatement state=null;
        ResultSet rs=null;
        //2  预编译SQL语句
        try {
            //3 填充参数
            state = connection.prepareStatement(SQL_LOGIN);
            state.setString(1,username);
            state.setString(2,password);
            rs=state.executeQuery();
            //根据结果返回真假
            return rs.next();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //释放资源
            DruidUtil.close(connection,state,rs);
        }
        return false;
    }
}
